#ifndef __COMM_HPP__
#define __COMM_HPP__

#include <iostream>
#include <string>
#include <cassert>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "log.hpp"

#define PATH_NAME "/home/JK/Linux/ShareMemory"
#define proj_ID 0x66
#define SIZE 4096

#define FIFO_NAME "./fifo"

#define READ O_RDONLY
#define WEITE O_WRONLY

class Init
{
public:
    Init()
    {
        umask(0);
        int ret=mkfifo(FIFO_NAME,0666);
        if(ret==-1)
        {
            logMessage(ERROR,"%s [%s][%d]","mkfifo error",__FILE__,__LINE__);
            exit(1);
        }
        logMessage(ERROR,"%s [%s][%d]","mkfifo sucess",__FILE__,__LINE__);
    }

    ~Init()
    {
        unlink(FIFO_NAME);
        logMessage(ERROR,"%s [%s][%d]","unlink sucess",__FILE__,__LINE__);
    }
};

void Wait(int fd)
{
    logMessage(NORMAL,"%s [%s][%d]","等待中....",__FILE__,__LINE__);
    uint32_t tmp=0;
    ssize_t sz=read(fd,&tmp,sizeof(tmp));
    assert(sz==sizeof(tmp));
    (void)sz;
}

void Signal(int fd)
{
    uint32_t tmp=1;
    ssize_t sz=write(fd,&tmp,sizeof(tmp));
    assert(sz==sizeof(tmp));
    (void)sz;
    logMessage(NORMAL,"%s [%s][%d]","唤醒中...",__FILE__,__LINE__);
}

int OpenFifo(std::string pathname,int flag)
{
    int fd=open(pathname.c_str(),flag);
    assert(fd>=0);
    return fd;
}

void CloseFifo(int fd)
{
    close(fd);
}

#endif